package service;

import mapper.BrandMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import pagination.PageBean;
import pojo.Brand;
import utils.SqlSessionFactoryUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;



public class BrandService {
    //品牌分页
    public PageBean<Brand> selectAllPage(int currentPage, int pageSize){
        //1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        //2.获取mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3.执行分页
        PageBean<Brand> pageBean = new PageBean<Brand>();

        int beginIndex = (currentPage - 1)*pageSize;
        List<Brand> brandList = brandMapper.selectAllPage(beginIndex,pageSize);

        int totalCount = brandMapper.selectCount();
        pageBean.setData(brandList);
        pageBean.setTotalCount(totalCount);
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);

        int totalPage = (int)(Math.ceil(totalCount*1.0/pageSize)) ;
        pageBean.setTotalPage(totalPage);

        //4.sqlSession关闭
        sqlSession.close();

        //5.返回数据
        return pageBean;

    }

    //品牌查询
    public List<Brand> selectAll(){
        //1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        //2.获取mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3.执行新增
        List<Brand> brandList = brandMapper.selectAll();
        //4.sqlSession关闭
        sqlSession.close();

        //5.返回数据
        return brandList;

    }
    //品牌新增
    public void add(Brand brand){
        //1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        //2.获取Mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3.执行新增
        brandMapper.add(brand);
        //4.提交事务
        sqlSession.commit();
        //5.sqlSession关闭
        sqlSession.close();
    }

    //单个查询
    public Brand selectById(int id){
        //1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        //2.获取Mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3.查询
        Brand brand = brandMapper.selectById(id);
        sqlSession.close();

        //5.返回数据
        return brand;

    }
    //品牌修改
    public void update(Brand brand){
        //1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        //2.获取Mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3.执行新增
        brandMapper.update(brand);
        //4.提交事务
        sqlSession.commit();
        //5.sqlSession关闭
        sqlSession.close();
    }
    //删除品牌
    public void delete(int id){
        //1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        //2.获取Mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3.执行新增
        brandMapper.delete(id);
        //4.提交事务
        sqlSession.commit();
        //5.sqlSession关闭
        sqlSession.close();
    }


}

